package com.epfresh.supplier.activity.standingbook;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.epfresh.api.constant.AppSupplyRouterConstant;
import com.epfresh.api.entity.RequestEntitySupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.utils.CustomDialog;
import com.epfresh.api.utils.InputControl;
import com.epfresh.api.utils.T;
import com.epfresh.api.widget.list.LoadMoreListView;
import com.epfresh.supplier.R;
import com.epfresh.supplier.activity.H5OrderDetailActivity;
import com.epfresh.supplier.adapter.DebtAdapter;
import com.epfresh.supplier.entity.CancelDebtEntity;
import com.epfresh.supplier.entity.DebtEntity;
import com.epfresh.supplier.entity.QrEntity;
import com.google.gson.Gson;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by shenjie on 2017/9/21.
 */

public class DebtActivity extends BaseActivity implements AdapterView.OnItemClickListener, LoadMoreListView.IXListViewListener, DebtAdapter.ImageClickListener {

    private RelativeLayout hint;
    private TextView cancelhintNumber, hintBottom;
    private EditText searchEt;
    private ImageView searchIv, deleteIv;
    private LoadMoreListView resultList;
    private ArrayList<DebtEntity.DebtListEntity> debtLists = new ArrayList<>();
    private DebtAdapter adapter;
    private LinearLayout llImage;
    private RelativeLayout selectAll;
    private ImageView selectImage;
    private TextView selectTv;
    private TextView tvTotal, cancelAccount;
    private boolean isSearch;
    private boolean hasCancel = false;
    private ArrayList<DebtEntity.DebtListEntity> searchLists = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        loadHttpRequest();
    }

    private void initView() {
        toolbarTitle.setText("欠款明细");
//        toolbarTitle.setTextColor(getResources().getColor(R.color.font_color_def));
//        float textsize = getResources().getDimension(R.dimen.text_size_max_18);
//        final float scale = getResources().getDisplayMetrics().density;
//        float size = (textsize / scale + 0.5f);
//        toolbarTitle.setTextSize(size);
        ivBack.setVisibility(View.VISIBLE);
        tvBack.setVisibility(View.VISIBLE);
        righticon.setVisibility(View.VISIBLE);
        righticon.setImageResource(R.mipmap.canceldebtrecord);
        righticon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(DebtActivity.this, CancelDebtRecordActivity.class);
                startActivity(intent);
            }
        });
        setContentView(R.layout.activity_debt);
        cancelAccount = (TextView) findViewById(R.id.cancelaccount);
        cancelAccount.setOnClickListener(this);
        tvTotal = (TextView) findViewById(R.id.tv_total);
        selectAll = (RelativeLayout) findViewById(R.id.rl_selectall);
        selectImage = (ImageView) findViewById(R.id.image_select);
        selectTv = (TextView) findViewById(R.id.tv_select);
        selectAll.setOnClickListener(this);
        llImage = (LinearLayout) findViewById(R.id.imagecon);
        searchEt = (EditText) findViewById(R.id.search_et);
        searchIv = (ImageView) findViewById(R.id.search_iv);
        deleteIv = (ImageView) findViewById(R.id.delete_iv);
        deleteIv.setVisibility(View.INVISIBLE);
        resultList = (LoadMoreListView) findViewById(R.id.employ_list);
        adapter = new DebtAdapter(this, debtLists);
        resultList.setAdapter(adapter);
        adapter.setListener(this);
        resultList.setOnItemClickListener(this);
        resultList.setXListViewListener(this);
        resultList.setPullLoadEnable(false);
        resultList.setVisibility(View.INVISIBLE);
        llImage.setVisibility(View.VISIBLE);
        searchIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (InputControl.isEmpty(searchEt)) {
                    T.toast("请输入用户姓名或手机号");
                    return;
                }
                isSearch = false;
                debtLists.clear();
                adapter.notifyDataSetChanged();
            }
        });
        deleteIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchEt.setText("");
            }
        });
        hint = (RelativeLayout) findViewById(R.id.cancel_debt_hint);
        cancelhintNumber = (TextView) findViewById(R.id.hint_tv2);
        hintBottom = (TextView) findViewById(R.id.tv_hint_bottom);
        hint.setVisibility(View.GONE);
        initDialog();
        changeCancelDebtState(false);

        searchEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.toString().equals("")){
                    deleteIv.setVisibility(View.INVISIBLE);
                }else {
                    deleteIv.setVisibility(View.VISIBLE);
                }
                searchLists.clear();
                if (!"".equals(s.toString().trim())) {
                    for (DebtEntity.DebtListEntity item : debtLists) {
                        if (item.getBuyerPhone().contains(s.toString().trim()) || item.getCustomerName().contains(s.toString().trim())) {
                            searchLists.add(item);
                        }
                    }
                    adapter.setList(searchLists);
                    adapter.notifyDataSetChanged();
                    if(searchLists.size()==0){
                        changeCancelDebtState(false);
                        llImage.setVisibility(View.VISIBLE);
                        resultList.setVisibility(View.INVISIBLE);
                    }else {
                        changeCancelDebtState(true);
                        llImage.setVisibility(View.INVISIBLE);
                        resultList.setVisibility(View.VISIBLE);
                    }
                } else {
                    adapter.setList(debtLists);
                    adapter.notifyDataSetChanged();
                    if(debtLists.size()==0){
                        changeCancelDebtState(false);
                        llImage.setVisibility(View.VISIBLE);
                        resultList.setVisibility(View.INVISIBLE);
                    }else {
                        changeCancelDebtState(true);
                        llImage.setVisibility(View.INVISIBLE);
                        resultList.setVisibility(View.VISIBLE);
                    }
                }
            }
        });
    }

    @Override
    protected void onToolbarBack() {
        Intent intent = new Intent();
        intent.putExtra("hasCancel", hasCancel);
        setResult(200, intent);
        finish();
    }

    int selectAllType = 0;

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.rl_selectall:
                if(!InputControl.isEmpty(searchEt)){
                    if(searchLists.size()==0){
                        return;
                    }
                }
                if (debtLists.size() == 0) {
                    return;
                }
                if (selectAllType == 0) {
                    selectAllType = 1;
                    selectImage.setImageResource(R.mipmap.select);
                    for (DebtEntity.DebtListEntity entity : debtLists) {
                        entity.setSelect(true);
                    }
                } else {
                    selectAllType = 0;
                    selectTv.setText("全选");
                    selectImage.setImageResource(R.mipmap.unselect_small);
                    for (DebtEntity.DebtListEntity entity : debtLists) {
                        entity.setSelect(false);
                    }
                }
                adapter.notifyDataSetChanged();
                float total = getTotal();
                tvTotal.setText("总计: " + total + "元");
                if (total > 0) {
                    changeCancelDebtState(true);
                } else {
                    changeCancelDebtState(false);
                }
                break;
            case R.id.ok_rl:
                loadCancelRequest();
                break;
            case R.id.cancel_rl:
                dialog.dismiss();
                break;
            case R.id.cancelaccount:
                showDialog();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//        debtLists.get(position).setSelect(!debtLists.get(position).isSelect());
//        adapter.notifyDataSetChanged();

//        Intent detail = new Intent(this, StandingBookDetailActivity.class);
//        detail.putExtra("orderId", ""+debtLists.get(position).getOrderId());
//        detail.putExtra(com.epfresh.api.constant.Constant.KEY_WEB_URL, "http://m.epfresh.com/help/order_supply_new.html?orderId=" + debtLists.get(position).getOrderId());
//        detail.putExtra(com.epfresh.api.constant.Constant.KEY_WEB_TITLE, "订单详情");
//        startActivity(detail);
        Intent intent = new Intent(this, StandingBookDetailActivity.class);

        intent.putExtra("orderId", adapter.getList().get(position).getOrderId() + "");
        intent.putExtra("debtFee", adapter.getList().get(position).getDebtFee() + "");
        startActivity(intent);

    }

    int currentNum = 0;

    private void loadHttpRequest() {
        RequestEntitySupply<Map<String, String>> data = new RequestEntitySupply<>();
        data.setCmd(AppSupplyRouterConstant.cmd_finance_payment_pos_findSupplyOrderDebtPage);
        data.setClientIdentifierCode(getAppApplication().getClientIdentifierCode());
        Map<String, String> map = new HashMap<>();
        map.put("storeId", getAppApplication().getStoreId());
//        map.put("storeId", "4");
        map.put("pageNumber", currentNum + "");
        map.put("pageSize", 1000 + "");
        if(getIntent().getStringExtra("queryDate")!=null&&!"".equals(getIntent().getStringExtra("queryDate")))
            map.put("queryDate",getIntent().getStringExtra("queryDate"));
        map.put("buyer", searchEt.getText().toString().trim());
        data.setParameters(map);
        request(data, AppSupplyRouterConstant.cmd_finance_payment_pos_findSupplyOrderDebtPage, onRequestListener);
    }

    private OnRequestListener<DebtEntity> onRequestListener = new OnRequestListener<DebtEntity>() {
        @Override
        public DebtEntity jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, DebtEntity.class);

        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onStart(Object tag) {
            if (!isSearch) {
                showProgressDialog();
            }

        }

        @Override
        public void onResponse(ResponseEntity<DebtEntity> response, Object tag) {
            hideProgressDialog();
            DebtEntity entity = response.getResponseElement();
            if (entity != null) {
                currentNum = entity.getNumber();
                if (entity.getContent().size() == 1000) {
                    currentNum++;
                }

                if (entity.isLast()) {
                    resultList.noMore();
                } else {
                    resultList.setPullLoadEnable(true);
                }
                debtLists.addAll(entity.getContent());
                if (debtLists.size() == 0) {
                    changeCancelDebtState(false);
                }else {
                    changeCancelDebtState(true);
                }

            }
            if (debtLists.size() > 0) {
                llImage.setVisibility(View.INVISIBLE);
                resultList.setVisibility(View.VISIBLE);
            } else {
                llImage.setVisibility(View.VISIBLE);
                resultList.setVisibility(View.INVISIBLE);
            }
            tvTotal.setText("总计: "  + "0.0元");
            adapter.notifyDataSetChanged();

        }


    };

    private float getTotal() {
        BigDecimal total = new BigDecimal("0");
        for (DebtEntity.DebtListEntity entity : debtLists) {
            if (entity.isSelect()) {
                total=total.add(new BigDecimal(entity.getDebtFee()+""));
            }
        }
        return total.floatValue();
    }


    @Override
    public void onLoadMore() {
        loadHttpRequest();
    }

    private RelativeLayout cancel, ok;
    private TextView totalDebt, sunshi;
    private EditText currentDebt, leaveDebt;
    private float totalFloat;
    private TextView okTv;
    CustomDialog dialog;

    private void initDialog() {
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_debt, null);
        totalDebt = (TextView) view.findViewById(R.id.totalDebt);
        currentDebt = (EditText) view.findViewById(R.id.currentDebt);
        leaveDebt = (EditText) view.findViewById(R.id.leave);
        sunshi = (TextView) view.findViewById(R.id.sunshi);
        cancel = (RelativeLayout) view.findViewById(R.id.cancel_rl);
        ok = (RelativeLayout) view.findViewById(R.id.ok_rl);
        okTv = (TextView) view.findViewById(R.id.ok_tv);
        dialog = new CustomDialog.Builder(this).setContentView(view).create();
        ok.setOnClickListener(this);
        cancel.setOnClickListener(this);
        currentDebt.addTextChangedListener(new TextWatcher() {
            private CharSequence temp;//监听前的文本
            private int editStart;//光标开始位置
            private int editEnd;//光标结束位置

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                temp = s;
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {

                String temp = s.toString();
                int posDot = temp.indexOf(".");
                if (posDot != -1 && temp.length() - posDot - 1 > 2) {
                    s.delete(posDot + 3, posDot + 4);
                    return;
                }
                editStart = currentDebt.getSelectionStart();
                editEnd = currentDebt.getSelectionEnd();
                if (!isFloat(s.toString())) {
                    if (s.toString().equals("")) {
                        setOkStatus(false);
                        return;
                    }
                    if (!"".equals(s.toString().trim())) {
                        if (editStart < 1) {
                            editStart = editEnd = 1;
                        }
                        s.delete(editStart - 1, editEnd);
                    }
                    int tempSelection = editStart;
                    currentDebt.setText(s);
                    if (tempSelection < 0) {
                        tempSelection = 0;
                    }
                    currentDebt.setSelection(tempSelection);
                    return;
                }
                BigDecimal a = new BigDecimal("0");
                BigDecimal b = new BigDecimal("0");
                if (!"".equals(s.toString().trim())) {
                    b = new BigDecimal(s.toString());
                }
                if ("".equals(s.toString().trim())) {
                    setOkStatus(false);
                }
                if (new Float(b.doubleValue()) > totalFloat) {
                    T.toast("输入数字不能超过总金额");
                    if (editStart < 1) {
                        editStart = editEnd = 1;
                    }
                    s.delete(editStart - 1, editEnd);
                    int tempSelection = editStart - 1;
                    currentDebt.setText(s);
                    if (tempSelection < 0) {
                        tempSelection = 0;
                    }
                    currentDebt.setSelection(tempSelection);
                } else {
                    leaveDebt.setText(new BigDecimal(totalFloat + "").subtract(b).doubleValue() + "");
                }

                if (!s.toString().equals("") && new BigDecimal(s.toString()).doubleValue() > 0 && !InputControl.isEmpty(leaveDebt)) {
                    setOkStatus(true);
                } else {
                    setOkStatus(false);
                }

            }
        });
        leaveDebt.addTextChangedListener(new TextWatcher() {
            private CharSequence temp;//监听前的文本
            private int editStart;//光标开始位置
            private int editEnd;//光标结束位置

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                temp = s;
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {

                String temp = s.toString();
                int posDot = temp.indexOf(".");
                if (posDot != -1 && temp.length() - posDot - 1 > 2) {
                    s.delete(posDot + 3, posDot + 4);
                    return;
                }

                editStart = leaveDebt.getSelectionStart();
                editEnd = leaveDebt.getSelectionEnd();
                if (!isFloat(s.toString())) {
                    if (s.toString().equals("")) {
                        setOkStatus(false);
                        return;
                    }
                    if (!"".equals(s.toString().trim())) {
                        if (editStart < 1) {
                            editStart = editEnd = 1;
                        }
                        s.delete(editStart - 1, editEnd);
                    }

                    int tempSelection = editStart;
                    if (tempSelection < 0) {
                        tempSelection = 0;
                    }
                    leaveDebt.setSelection(tempSelection);
                    return;
                }
                BigDecimal a = new BigDecimal("0");
                if (!"".equals(currentDebt.getText().toString())) {
                    a = new BigDecimal(currentDebt.getText().toString());
                }
                BigDecimal b = new BigDecimal("0");
                if (!"".equals(s.toString().trim())) {
                    b = new BigDecimal(s.toString());
                }
                if ("".equals(s.toString().trim())) {
                    setOkStatus(false);
                }
                if ((new Float(a.add(b).toPlainString()).floatValue()) > totalFloat) {
                    T.toast("输入数字不能超过总金额");

                    if (editStart < 1) {
                        editStart = editEnd = 1;
                    }
                    s.delete(editStart - 1, editEnd);
                    int tempSelection = editStart - 1;
                    if (tempSelection < 0) {
                        tempSelection = 0;
                    }
                    leaveDebt.setText(s);

                    leaveDebt.setSelection(tempSelection);
                } else {
                    sunshi.setText(new BigDecimal(totalFloat + "").subtract(a).subtract(b).doubleValue() + "元");
                }

                if (!s.toString().equals("") && !InputControl.isEmpty(currentDebt) && new BigDecimal(currentDebt.getText().toString().trim()).doubleValue() > 0) {
                    setOkStatus(true);
                } else {
                    setOkStatus(false);
                }

            }
        });

    }

    private void showDialog() {
        totalFloat = getTotal();
        totalDebt.setText(totalFloat + "元");
        currentDebt.setText(totalFloat + "");
        dialog.show();
    }

    private void changeCancelDebtState(boolean can) {
        if (can) {
            cancelAccount.setBackgroundColor(getResources().getColor(R.color.red));
            cancelAccount.setClickable(true);
        } else {
            cancelAccount.setBackgroundColor(getResources().getColor(R.color.aaaaaa));
            cancelAccount.setClickable(false);
        }
    }

    public boolean isFloat(String str) {
        try {
            if ("".equals(str)) {
                return true;
            }
            Float.parseFloat(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }


    private void loadCancelRequest() {
        RequestEntitySupply<Map<String, Object>> data = new RequestEntitySupply<>();
        data.setCmd(AppSupplyRouterConstant.cmd_finance_payment_pos_squareBill);
        data.setClientIdentifierCode(getAppApplication().getClientIdentifierCode());
        Map<String, Object> map = new HashMap<>();
        map.put("storeId", getAppApplication().getStoreId());
        ArrayList<String> arrayList = new ArrayList<>();
        if(!InputControl.isEmpty(searchEt)){
            for (DebtEntity.DebtListEntity item : searchLists) {
                if (item.isSelect()) {
                    arrayList.add(item.getOrderId() + "");
                }
            }
        }else {
            for (DebtEntity.DebtListEntity item : debtLists) {
                if (item.isSelect()) {
                    arrayList.add(item.getOrderId() + "");
                }
            }
        }

        map.put("ids", arrayList);
        map.put("type", 0 + "");
        map.put("payFee", currentDebt.toString());
        map.put("debtFee", leaveDebt.toString());
        if (sunshi != null && sunshi.getText().toString().trim().length() > 1) {
            map.put("coupon", sunshi.getText().toString().substring(0, sunshi.getText().toString().length() - 1));
        } else {
            map.put("coupon", "0");
        }

        map.put("merchantId", getAppApplication().getMerchantId());
        float current = 0;
        if (InputControl.isEmpty(currentDebt)) {
            T.toast("本次收款金额不能为空");
            return;
        } else {
            map.put("payFee", currentDebt.getText().toString().trim());
            current = new Float(currentDebt.getText().toString().trim()).floatValue();
        }
        float le = 0;
        if (InputControl.isEmpty(leaveDebt)) {
            T.toast("剩余欠款金额不能为空");
        } else {
            map.put("debtFee", leaveDebt.getText().toString().trim());
            le = new Float(leaveDebt.getText().toString().trim()).floatValue();
        }
        data.setParameters(map);
        request(data, AppSupplyRouterConstant.cmd_finance_payment_pos_squareBill, cancelDebtListener);
    }


    private OnRequestListener<CancelDebtEntity> cancelDebtListener = new OnRequestListener<CancelDebtEntity>() {
        @Override
        public CancelDebtEntity jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, CancelDebtEntity.class);

        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onStart(Object tag) {
            showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<CancelDebtEntity> response, Object tag) {
            hideProgressDialog();
            CancelDebtEntity entity = response.getResponseElement();
            if (!"0".equals(entity.getResult())) {
                T.toast(entity.getMsg());
            } else {
                dialog.dismiss();
//                ArrayList<DebtEntity.DebtListEntity> list=new ArrayList();
//                for (DebtEntity.DebtListEntity item:debtLists){
//                    if(!item.isSelect()){
//                        list.add(item);
//                    }
//                }
//                debtLists.clear();
//                debtLists.addAll(list);
//                adapter.notifyDataSetChanged();


                if(entity.getQrInfo().getPayFee()!=0){
                    loadCashPayment(entity.getQrInfo().getBatchReceiptNumber());
                }
            }

        }


    };

    private OnRequestListener<CancelDebtEntity> qrListener = new OnRequestListener<CancelDebtEntity>() {
        @Override
        public CancelDebtEntity jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, CancelDebtEntity.class);

        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {

        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {

        }

        @Override
        public void onStart(Object tag) {

        }

        @Override
        public void onResponse(ResponseEntity<CancelDebtEntity> response, Object tag) {
            CancelDebtEntity entity = response.getResponseElement();
            hasCancel = true;
            hint.setVisibility(View.VISIBLE);
            DecimalFormat format = new DecimalFormat("0.00");
            if(entity.getPosPayInfo()!=null){
                cancelhintNumber.setText(entity.getPosPayInfo().getPayFee() + "");
                hintBottom.setText("(结账优惠:" + format.format(entity.getPosPayInfo().getCouponFee()) + "元)");
            }else {
                cancelhintNumber.setText(entity.getQrInfo().getPayFee() + "");
                hintBottom.setText("(结账优惠:" + 0 + "元)");
            }


            handler.sendEmptyMessageDelayed(1, 2000);
        }


    };

    private void loadCashPayment(String batchId){
        RequestEntitySupply<Map<String, Object>> data = new RequestEntitySupply<>();
        data.setCmd(AppSupplyRouterConstant.cmd_finance_payment_pos_cashPayment);
        data.setClientIdentifierCode(getAppApplication().getClientIdentifierCode());
        Map<String, Object> map = new HashMap<>();
        map.put("batchReceiptNumber", batchId);
        map.put("merchantId",getAppApplication().getMerchantId());
        data.setParameters(map);
        request(data, AppSupplyRouterConstant.cmd_finance_payment_pos_cashPayment, qrListener);

    }



    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 10) {
                currentNum = 0;
                debtLists.clear();
                adapter.notifyDataSetChanged();
                isSearch = true;
                loadHttpRequest();
                return;
            }
            currentNum = 0;
            debtLists.clear();
            loadHttpRequest();
            selectAllType = 0;
            selectTv.setText("全选");
            selectImage.setImageResource(R.mipmap.unselect_small);
            for (DebtEntity.DebtListEntity entity : debtLists) {
            entity.setSelect(false);
}
            hint.setVisibility(View.GONE);

        }
    };


    @Override
    public void slectClick() {
        float total = getTotal();
        tvTotal.setText("总计: " + total + "元");
        if(!InputControl.isEmpty(searchEt)){
            boolean hasNoselect=false;
            for(DebtEntity.DebtListEntity item:searchLists){
               if(!item.isSelect()) {
                   hasNoselect=true;
                   break;
               }
            }

            if (!hasNoselect) {
                selectAllType = 1;
                selectImage.setImageResource(R.mipmap.select);
            } else {
                selectAllType = 0;
                selectImage.setImageResource(R.mipmap.unselect_small);
            }
        }else {
            boolean hasNoselect=false;
            for(DebtEntity.DebtListEntity item:debtLists){
                if(!item.isSelect()) {
                    hasNoselect=true;
                    break;
                }
            }

            if (!hasNoselect) {
                selectAllType = 1;
                selectImage.setImageResource(R.mipmap.select);
            } else {
                selectAllType = 0;
                selectImage.setImageResource(R.mipmap.unselect_small);
            }
        }
        if (total > 0) {
            changeCancelDebtState(true);
        } else {
            changeCancelDebtState(false);
        }
    }

    private void setOkStatus(boolean canClick) {
        if (canClick) {
            okTv.setTextColor(getResources().getColor(R.color.red));
        } else {
            okTv.setTextColor(0xffefefef);
        }
        ok.setClickable(canClick);
    }
}
